package cn.kgm.bio.threadPoolLink;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        try {
            ServerSocket server = new ServerSocket(6666);
            HandleSocketThreadPool handleSocketThreadPool = new HandleSocketThreadPool(3, 1000);
            while (true) {
                Socket socket = server.accept();
                System.out.println("有人上线了！");
                handleSocketThreadPool.execute(new ReaderClientRunnable(socket));
            }
        } catch (Exception e) {
            System.out.println("创建Socket失败！");
            e.printStackTrace();
        }

    }

    private static class ReaderClientRunnable implements Runnable {

        Socket socket;

        ReaderClientRunnable(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                InputStream is = this.socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String line;
                while ((line = br.readLine()) != null) {
                    Thread thread = Thread.currentThread();
                    System.out.println("服务端阅读到的消息: " + line + "threadName = " + thread.getName());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
